package com.tbit.uqbike.compalint.service.quartz;

import com.tbit.uqbike.compalint.enums.PlatformEnum;
import com.tbit.uqbike.compalint.param.ComplaintPullParam;
import com.tbit.uqbike.compalint.service.base.AbstractComplaintService;
import com.tbit.uqbike.compalint.service.factory.ComplaintFactory;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * 客诉拉取定时任务
 *
 * @author <a href="mailto:zhang.zhiliang@tbit.com.cn">Zhang ZhiLiang</a>
 * @version 1.0
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a>
 * @since 2025/2/19
 */
@Slf4j
@AllArgsConstructor
@Component
public class ComplaintPullQuartz {

    private final ComplaintFactory complaintFactory;
    private final AccountDao accountDao;
    private final List<PlatformEnum> pullPlatform = Arrays.asList(PlatformEnum.WECHAT,PlatformEnum.ZFB,PlatformEnum.ADAPAY);

    /**
     * 每小时一次
     * 已迁移到定时任务，留作主动触发
     */
//    @Scheduled(cron = "30 59 0/1 * * ?")
    public void pull(){
        for (PlatformEnum platformEnum : pullPlatform) {
            AbstractComplaintService complaintService = complaintFactory.getComplaintService(platformEnum);
            // 查询所有品牌
            List<Account> allAccount = accountDao.getAllAccount(1);
            for (Account account : allAccount) {
                Integer brandId = account.getAccountId();
                complaintService.complaintPull(getDefaultParam(brandId,platformEnum.getCode(),complaintService.getSize(brandId)));
            }
        }
    }

    /**
     * 默认拉取配置
     * 每小时拉取20条
     * @param brandId 品牌id
     * @param currentSum 当前时刻的数据库条目总数
     * @return 默认拉取参数
     */
    private ComplaintPullParam getDefaultParam(Integer brandId, Integer platform, Long currentSum){
        String[] timeArr = getNowDayTimeRange();
        // 支付宝最大值为20
        return new ComplaintPullParam(brandId,timeArr[0],timeArr[1],20,currentSum.intValue(),platform);
    }

    /**
     * 获取前一天的起始时间和结束时间
     *
     * @return 一个包含前一天起始时间和结束时间的字符串数组
     */
    public static String[] getNowDayTimeRange() {
        // 获取当前时间
        ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());

        // 获取当天的日期
        ZonedDateTime previousDayStart = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
        ZonedDateTime previousDayEnd = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 格式化时间为 "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String start = previousDayStart.format(formatter);
        String end = previousDayEnd.format(formatter);

        return new String[]{start, end};
    }

}
